package com.suxin.splitter.spring;

import com.suxin.splitter.Split;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.Method;

/**
 * @author Tang
 * @classname SplitInvokerProcessor
 * @description [  ]
 * @date 2022/11/9 16:10
 */
public class SplitInvokerProcessor implements BeanPostProcessor {

    private SplitInvokerRegister splitInvokerRegister;

    public SplitInvokerProcessor(SplitInvokerRegister splitInvokerRegister) {
        this.splitInvokerRegister = splitInvokerRegister;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        processor(bean);
        return bean;
    }

    private void processor(Object bean) {
        Class<?> targetClass = AopUtils.getTargetClass(bean);
        MethodUtils.getMethodsListWithAnnotation(targetClass, Split.class).forEach((method) -> {
            Split split = method.getAnnotation(Split.class);
            SplitService splitService = buildSingleParamSplitInvoker(split,method);
            this.splitInvokerRegister.register(method,splitService);
        });
    }

    private SplitService buildSingleParamSplitInvoker(Split split, Method method) {
        return null;
    }

}
